package com.bincloud.vue.admin.web.config;

import com.bincloud.vue.admin.web.component.JwtTokenFilter;
import com.bincloud.vue.admin.web.component.PasswordEncoder;
import com.bincloud.vue.admin.web.component.RestDeniedHandler;
import com.bincloud.vue.admin.web.component.RestEntryPoint;
import com.bincloud.vue.admin.web.util.security.VueAdminUserDetailsService;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

import javax.annotation.Resource;

/**
 * 权限配置
 * @author bincloud, mvpzhou
 */
@Configuration
@EnableWebSecurity
// 允许注解拦截
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Resource
    private JwtTokenFilter jwtTokenFilter;
    @Resource
    private RestEntryPoint restEntryPoint;
    @Resource
    private RestDeniedHandler restDeniedHandler;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private VueAdminUserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.httpBasic();
        // csrf 禁用
        http.csrf().disable();
        // session 禁用
        http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
        // 静态资源白名单
        http.authorizeHttpRequests().antMatchers(
                HttpMethod.GET,
                "/",
                "/favicon.ico",
                "/**/*.html",
                "/**/*.css",
                "/**/*.js",
                "/doc.html",
                "/webjars/**",
                "/swagger-resources/**",
                "/swagger-ui/**",
                "/v3/api-docs/**"
                ).permitAll();
        // 接口资源白名单
        http.authorizeHttpRequests().antMatchers("/api/auth/**").permitAll();
        http.authorizeHttpRequests().antMatchers("/api/captcha/**").permitAll();
        http.authorizeHttpRequests().antMatchers("/api/static/**").permitAll();
        // 跨域请求
        http.authorizeHttpRequests().antMatchers(HttpMethod.OPTIONS).permitAll();
        // 其余请求 需授权访问
        http.authorizeHttpRequests().anyRequest().authenticated();
        // 缓存禁用
        http.headers().cacheControl();
        // jwt 过滤器
        http.addFilterBefore(jwtTokenFilter, UsernamePasswordAuthenticationFilter.class);
        // 自定义非法请求处理类
        http.exceptionHandling()
                .accessDeniedHandler(restDeniedHandler)
                .authenticationEntryPoint(restEntryPoint);
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder);
    }

    @Override
    public UserDetailsService userDetailsService() {
        return username -> userDetailsService.loadUserByUsername(username);
    }
}
